package com.java2007.zhangyu.restaurant.controller;

import com.java2007.zhangyu.restaurant.pojo.FoodType;
import com.java2007.zhangyu.restaurant.service.FoodTypeService;
import com.java2007.zhangyu.restaurant.service.impl.FoodTypeServiceImpl;
import com.java2007.zhangyu.restaurant.utils.WebUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * @author zy
 * @Version
 * @date 2021/01/23
 */
public class FoodTypeControllerV1 extends HttpServlet {

    private FoodTypeService foodTypeService = new FoodTypeServiceImpl();

    public void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //获取请求参数
            String fTypeIdStr = request.getParameter("fTypeId");
            //将字符串类型转换为整型
            int fTypeId = Integer.parseInt(fTypeIdStr);
            //根据菜类别名模糊查询菜类别的信息
            foodTypeService.deleteByFoodTypeId(fTypeId);
            //重定向到查询接口
            response.sendRedirect(request.getContextPath() + "/foodtype?method=search");
        } catch (Exception e) {
            e.printStackTrace();
            request.getRequestDispatcher("../../backend/index.jsp").forward(request, response);
        }
    }

    public void queryByFtId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //获取请求参数
            String fTypeIdStr = request.getParameter("fTypeId");
            //将字符串类型转换为整型
            int fTypeId = Integer.parseInt(fTypeIdStr);
            //根据菜类别名id查询菜类别的信息
            FoodType foodType = foodTypeService.queryByFoodTypeId(fTypeId);
            //将查询到的信息放到 request域中
            request.setAttribute("type",foodType);
            //转发
            request.getRequestDispatcher("/backend/detail/foodtype/food-type-update.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.getRequestDispatcher("../../backend/index.jsp").forward(request, response);
        }
    }

    public void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //获取请求参数
            String fTypeName = request.getParameter("fTypeName");
            //根据菜品类别名增加菜品类别
            foodTypeService.saveByFoodTypeName(fTypeName);
            //重定向到查询接口
            response.sendRedirect(request.getContextPath() + "/foodtype?method=search");
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("errorMsg", "服务器正忙，请稍后重试！");
        }
    }

    public void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //获取请求参数
            String fTypeName = request.getParameter("fTypeName");
            //根据菜类别名模糊查询菜类别的信息
            List<FoodType> types = foodTypeService.queryByFoodTypeName(fTypeName);
            //将查询到的信息放入request域中
            request.setAttribute("types",types);
            request.setAttribute("fTypeName",fTypeName);
            //转发
            request.getRequestDispatcher("/backend/detail/foodtype/food-type-list.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("errorMsg", "服务器正忙，请稍后重试！");
            request.getRequestDispatcher("../../backend/index.jsp").forward(request, response);
        }
    }

    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //获取请求多个参数，调用工具类 WebUtil
            FoodType foodType = WebUtil.copyParamToBean(request.getParameterMap(), new FoodType());
            //根据菜类别对象更改菜类别的信息
            foodTypeService.updateByFoodTypeName(foodType);
            //重定向到查询接口
            response.sendRedirect(request.getContextPath() + "/foodtype?method=search");
        } catch (Exception e) {
            e.printStackTrace();
            request.getRequestDispatcher("../../backend/index.jsp").forward(request, response);
        }

    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*
         * 代码优化一：
         *      同一模块的 Servlet过多，且 Servlet中的内容大抵相同
         *      因此，可将同一模块的多个 Servlet下的代码封装为不同方法放在同一Servlet下
         *      通过 if判断选择需要用到的方法
         *  缺点：
         *      当模块中的功能过多时，产生了大量的判断，修改需要修改判断语句
         */
        //获取请求参数
//        String method = request.getParameter("method");
//        if ("search".equals(method)){
//            foodTypeSearch(request,response);
//        } else if ("save".equals(method)){
//            foodTypeSave(request,response);
//        } else if ("update".equals(method)){
//            foodTypeUpdate(request,response);
//        } else if ("del".equals(method)){
//            foodTypeDeleteTypeId(request,response);
//        } else if ("querybyftid".equals(method)){
//            foodTypeQueryByFoodTypeId(request,response);
//        } else {
//            response.getWriter().write("未找到该方法");
//        }

        /*
         *代码优化二：
         *      通过反射获取 method的值，并调用其值对应的业务、方法
         *      下述代码可封装为 BaseServlet;
         */
//      String methodStr = request.getParameter("method");
//        try {
//            //获取 method业务鉴别字符串，获取相应的业务、方法(通过反射获取)
//            Method method = this.getClass().getDeclaredMethod(methodStr, HttpServletRequest.class, HttpServletResponse.class);
//            //调用目标业务 方法
//            method.invoke(this,request,response);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
